#! /bin/sh

prefix=@prefix@
exec_prefix=@exec_prefix@

# %%$@%$# why oh why isn't it $sharedir/monodoc
monodocdir=$prefix/lib/monodoc

case x$1 in
    x--assemble)
	shift;
	echo "--assemble is deprecated. Use \`mdoc-assemble'." 1>&2
	exec mdassembler "$@"
	;;
    x--get-sourcesdir)
	echo "--get-sourcesdir is deprecated. Use \`pkg-config monodoc --variable=sourcesdir'." 1>&2
	exec pkg-config monodoc --variable=sourcesdir
	;;
    x--normalize)
	shift;
	echo "--normalize is no longer supported.  Use \`xmllint'." 1>&2
	exit 1;
	;;
    x--update)
	echo "--update does not work.  Use \`mdoc-update' instead." 1>&2
	exit 1
	;;
    x--cs-to-ecma)
	shift;
	echo "--cs-to-ecma does not work. Use \`mdoc-update' with the \`--import' option." 1>&2
	exit 1;
	;;
    x--validate)
	shift;
	echo "--validate is deprecated. Use \`mdoc-validate'." 1>&2
	exec mdvalidater "$@"
	;;
    x--help)
	echo "Usage: monodoc [OPTION]... [TOPIC]"
	echo ""
	echo "Display documentation for TOPIC (or display the default start page"
	echo "if no TOPIC is provided).  TOPICs are ECMA-334 Annex E.3.1 CREFs, "
	echo "e.g. N:System, T:System.Object, M:System.Object.Equals, P: for"
	echo "properties, F: for fields, E: for events, etc.  See mdoc(5) "
	echo "CREF FORMAT for more details."
	echo ""
	echo "OPTION is one of the following:"
	echo "  --help             	Print this message"
	echo "  --about            	About the monodoc browser"
	echo "  --html TOPIC       	Print the HTML contents of TOPIC"
	echo "  --make-index       	Create the documentation index"
 	echo "  --make-search-index	Create the searchable documentation index"
	echo "  --engine ENGINE   	Select a browser backend"
	echo "                    	(Gecko, GtkHtml, WebKit, MonoWebBrowser)"
	echo
	echo "The following options are available for authoring documentation:"
	echo "  --edit path        Edit (unassembled) documentation at path"
	echo "  --merge-changes CHANGE_FILE [TARGET_DIR]"
	exit 0
	;;
esac

getdirectory () {
    w=`which $1`
    if test -h $w; then
	(cd `dirname $w`/`dirname \`readlink $w\``; pwd)
    else
	dirname $w
    fi
}

if test -n "$MOZILLA_FIVE_HOME"; then
	MOZILLA_HOME=$MOZILLA_FIVE_HOME
elif test x@MOZILLA_HOME@ != x; then
    if [ -f @MOZILLA_HOME@/chrome/comm.jar ]; then
        MOZILLA_HOME=@MOZILLA_HOME@
    fi 
elif grep -qs GRE_PATH /etc/gre.d/*.conf > /dev/null ; then
	MOZILLA_HOME=$(grep -h GRE_PATH= /etc/gre.d/*.conf | cut -d '"' -f 2 -d = | head -n 1)
elif [ $(which xulrunner 2> /dev/null) ] > /dev/null ; then
    MOZILLA_FIVE_HOME=`getdirectory xulrunner`
    MOZILLA_HOME=$MOZILLA_FIVE_HOME
elif [ $(which mozilla 2> /dev/null) ] && grep MOZILLA_FIVE_HOME= "$(which mozilla)" > /dev/null ; then
	MOZILLA_HOME=$(grep MOZILLA_FIVE_HOME= $(which mozilla) | cut -d '"' -f 2)
elif [ $(which firefox 2> /dev/null) ] && grep MOZILLA_FIVE_HOME= "$(which firefox)" > /dev/null ; then
	MOZILLA_HOME=$(grep MOZILLA_FIVE_HOME= $(which firefox) | cut -d '"' -f 2)
fi

if [ -n $LD_LIBRARY_PATH ]; then
	export LD_LIBRARY_PATH=$MOZILLA_HOME:$LD_LIBRARY_PATH
else
	export LD_LIBRARY_PATH=$MOZILLA_HOME
fi
export MOZILLA_FIVE_HOME 
export MOZILLA_HOME 
export LD_LIBRARY_PATH

exec @RUNTIME@ $monodocdir/browser.exe $OPTIONS "$@" $REST
